package com.health.content.controller;

import java.util.List;
import java.util.Map;

import com.health.common.constant.CacheConstants;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.health.common.annotation.Log;
import com.health.common.core.controller.BaseController;
import com.health.common.core.domain.AjaxResult;
import com.health.common.enums.BusinessType;
import com.health.content.domain.ContentCategory;
import com.health.content.service.IContentCategoryService;
import com.health.common.utils.poi.ExcelUtil;

/**
 * 栏目管理Controller
 * 
 * @author moxxie
 * @date 2025-10-27
 */
@RestController
@RequestMapping("/content/category")
public class ContentCategoryController extends BaseController
{
    @Autowired
    private IContentCategoryService contentCategoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询栏目管理列表
     */
    @PreAuthorize("@ss.hasPermi('content:category:list')")
    @GetMapping("/list")
    public AjaxResult list(ContentCategory contentCategory)
    {
        List<ContentCategory> list = contentCategoryService.selectContentCategoryList(contentCategory);
        return success(list);
    }

    /**
     * 导出栏目管理列表
     */
    @PreAuthorize("@ss.hasPermi('content:category:export')")
    @Log(title = "栏目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContentCategory contentCategory)
    {
        List<ContentCategory> list = contentCategoryService.selectContentCategoryList(contentCategory);
        ExcelUtil<ContentCategory> util = new ExcelUtil<ContentCategory>(ContentCategory.class);
        util.exportExcel(response, list, "栏目管理数据");
    }

    /**
     * 获取栏目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('content:category:query')")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return success(contentCategoryService.selectContentCategoryByCategoryId(categoryId));
    }

    /**
     * 新增栏目管理
     */
    @PreAuthorize("@ss.hasPermi('content:category:add')")
    @Log(title = "栏目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContentCategory contentCategory)
    {
        redisTemplate.opsForSet().add(CacheConstants.TMP_VALID_FILE_SET, contentCategory.getIcon());
        return toAjax(contentCategoryService.insertContentCategory(contentCategory));
    }

    /**
     * 修改栏目管理
     */
    @PreAuthorize("@ss.hasPermi('content:category:edit')")
    @Log(title = "栏目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContentCategory contentCategory)
    {
        redisTemplate.opsForSet().add(CacheConstants.TMP_VALID_FILE_SET, contentCategory.getIcon());
        return toAjax(contentCategoryService.updateContentCategory(contentCategory));
    }

    /**
     * 删除栏目管理
     */
//    @PreAuthorize("@ss.hasPermi('content:category:remove')")
//    @Log(title = "栏目管理", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{categoryIds}")
//    public AjaxResult remove(@PathVariable Long[] categoryIds)
//    {
//        return toAjax(contentCategoryService.deleteContentCategoryByCategoryIds(categoryIds));
//    }
    /**
     * 删除栏目管理
     */
    @PreAuthorize("@ss.hasPermi('content:category:remove')")
    @Log(title = "栏目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryId}")
    public AjaxResult remove(@PathVariable Long categoryId)
    {
        return toAjax(contentCategoryService.deleteContentCategoryByCategoryId(categoryId));
    }

    @PreAuthorize("@ss.hasPermi('content:category:edit')")
    @Log(title = "栏目管理", businessType = BusinessType.UPDATE)
    @PutMapping("/status")
    public AjaxResult updateStatus(@RequestBody Map<String, Object> params)
    {
        Long categoryId = Long.valueOf(params.get("categoryId").toString());
        String status = params.get("status").toString();
        Boolean recursive = Boolean.valueOf(params.get("recursive").toString());

        return toAjax(contentCategoryService.updateContentCategoryStatus(categoryId, status, recursive));
    }
}
